package top.gcl.controller;




import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;
import top.gcl.entity.Goods;
import top.gcl.madel.JsonResult;
import top.gcl.madel.JsonResultPage;
import top.gcl.service.GoodsServiceImpl;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Map;


@RestController
public class GoodsCtrl {
    @Resource
    private GoodsServiceImpl GoodsServiceImpl;

   @GetMapping("/goods/{id}")
   public JsonResult getGoodsById(@PathVariable String id) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
       JsonResult res=new JsonResult(0,"⭐查询成功⭐");
       Goods goods=GoodsServiceImpl.getGoodsById(id);
       if(goods==null)
       {
           res.setCode(1);
           res.setMsg("没有数据");
       }else
           {
               Map map= BeanUtils.describe(goods);
               SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
               String CreateTime=sdf.format(goods.getCreateTime());
               map.remove("class");
               map.put("createTime",CreateTime);
               res.setData(map);
           }
       return res;
   }
    //url后面没有/，client发请求时有没有/都可以。如果这里有/，那么client发请求时必须带上/
    @PostMapping(value="/sendGoods")
    public JsonResult sendGoods( Goods goods){
        GoodsServiceImpl.sendNotice(goods);
        return new JsonResult(200,"添加商品成功！");
    }
    @PostMapping("/getGoodsAll/{pageNum}/{pageSize}")
    public JsonResult list(@PathVariable  String pageNum ,@PathVariable String pageSize) throws Exception {
        int pageNumInt=0;
        int pageSizeInt=5;
        if(pageNum!=null&&!"".equals(pageNum));
        {
            try{

                pageNumInt=Integer.parseInt(pageNum);
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        if(pageSize!=null&&!"".equals(pageSize));
        {
            try
            {
                pageSizeInt=Integer.parseInt(pageSize);

            }catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        JsonResultPage res =new JsonResultPage(0,"查询成功");
        try {
            PageInfo<Goods> pageInfo=GoodsServiceImpl.list(pageNumInt,pageSizeInt);
            res.setPageNum(pageInfo.getPageNum());
            res.setPages(pageInfo.getPages());
            res.setPageSize(pageInfo.getPageSize());
            res.setTotal(pageInfo.getTotal());
            res.setData(pageInfo.getList());
        }catch (Exception e)
        {
            e.printStackTrace();
            res.setCode(100);
            res.setMsg("查询失败");
        }
        return res;
        //throw new Exception("模拟抛出非运行时异常");
    }
        @GetMapping("/like/{id}")
    public JsonResult like(@PathVariable String id)
        {
            JsonResult res=new JsonResult(0,"♥♥♥♥♥♥");
            int like=GoodsServiceImpl.like(id,1);
            res.setData(like);
            return res;
        }

     @GetMapping("/cancelike/{id}")
    public JsonResult cancelike(@PathVariable String id)
     {
         JsonResult res=new JsonResult(0,"-♥");
         int like=GoodsServiceImpl.like(id,-1);
         res.setData(like);
         return res;
     }

}
